package com.tyao.note.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tyao.note.mapper.BNoteMapper;
import com.tyao.note.mapper.BTagMapper;
import com.tyao.note.model.BNote;
import com.tyao.note.model.BTag;

@Service
public class NoteService {

	@Autowired
	BTagMapper tag;

	@Autowired
	BNoteMapper note;

	/**
	 * 笔记统计
	 * 
	 * @param userId
	 * @return
	 */
	public Map notesTotal(int userId) {
		Map totalMap = new HashMap<String, Integer>();
		int notesNum = note.notesTotal(userId);
		int todayNote = note.todayNotes(userId);
		int yesterdayNote = note.yesterdayNotes(userId);
		int weekNote = note.weekNotes(userId);
		int monthNote = note.monthNotes(userId);
		totalMap.put("notesNum", notesNum);
		totalMap.put("todayNote", todayNote);
		totalMap.put("yesterdayNote", yesterdayNote);
		totalMap.put("weekNote", weekNote);
		totalMap.put("monthNote", monthNote);
		return totalMap;
	}

	/**
	 * 分类统计
	 * 
	 * @param userId
	 * @return
	 */
	public Map tagsTotal(int userId) {
		Map totalMap = new HashMap();
		int tagsNum = tag.tagsTotal(userId);
		int todayTag = tag.todayTags(userId);
		int yesterdayTag = tag.yesterdayTags(userId);
		int weekTag = tag.weekTags(userId);
		int monthTag = tag.monthTags(userId);
		totalMap.put("tagsNum", tagsNum);
		totalMap.put("todayTag", todayTag);
		totalMap.put("yesterdayTag", yesterdayTag);
		totalMap.put("weekTag", weekTag);
		totalMap.put("monthTag", monthTag);
		return totalMap;
	}

	/**
	 * notes列表
	 * 
	 * @return
	 */
	public List<Map> selectNotes(Integer noteState,String tagId, String beginTime, String endTime, String noteTitle, int userId) {
		Map map = new HashMap();
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd
		// HH:mm:ss");//定义要输出日期字符串的格式
		if (tagId != null && tagId != "") {
			map.put("tagId", Integer.parseInt(tagId));
		}
		if (beginTime != null && beginTime != "") {
			// Date begin= sdf.parse(beginTime+"00:00:00");
			map.put("beginTime", beginTime + " 00:00:00");
		}
		if (endTime != null && endTime != "") {
			map.put("endTime", endTime + " 23:59:59");
		}
		map.put("title", noteTitle);
		map.put("userId", userId);
		map.put("noteState", noteState);
		return note.selNotes(map);
	}

	/** 
	 * 日志批量删除，单个删除
	 * 
	 * @param ids
	 * @return
	 */
	public int noteDelete(String ids) {
		List<String> list = new ArrayList<String>();
		String[] str = ids.split(",");
		for (int i = 0; i < str.length; i++) {
			list.add(str[i]);
		}
		return note.deleteByIds(list);
	}

	/**
	 * 按用户查询所有分类
	 * 
	 * @param userId
	 * @return
	 */
	public List<BTag> selClass(int userId) {
		return tag.selectAllByUser(userId);
	}

	/**
	 * 根据BTag对象中的值查询分类
	 * 
	 * @param record
	 * @return
	 */
	public List<BTag> selectClass(BTag record) {
		return tag.selectClass(record);
	}

	/**
	 * 根据id查询分类
	 * 
	 * @param id
	 * @return
	 */
	public BTag selClassById(int id) {
		return tag.selectByPrimaryKey(id);
	}

	/**
	 * 获取所有的tag，包括父子类
	 * 
	 * @param userid
	 * @return
	 */
	public List<BTag> getAllTag(int userid) {
		List<BTag> tag = selClass(userid);
		List<BTag> result = new ArrayList<BTag>();

		for (int i = 0; i < tag.size(); i++) {
			if (tag.get(i).getParentId() == 0) {
				result.add(tag.get(i));
			}
		}
		findTagChild(result, tag);
		return result;
	}

	public void findTagChild(List<BTag> result, List<BTag> tag) {
		for (int j = 0; j < result.size(); j++) {
			BTag item = result.get(j);
			int id = item.getId();
			List<BTag> chilList = new ArrayList<BTag>();
			for (int k = 0; k < tag.size(); k++) {
				BTag tagItem = tag.get(k);
				if (tagItem.getParentId() == id) {
					chilList.add(tagItem);
				}
			}
			if (chilList.size() > 0)
				item.setChilds(chilList);
			findTagChild(chilList, tag);
		}
	}

	/**
	 * 根据id查询父类
	 * 
	 * @param id
	 * @return
	 */
	public BTag selParentClass(int id) {
		BTag tag2;
		BTag tag1 = selClassById(id);
		int parentId = tag1.getParentId();
		// if(parentId!=0) {
		tag2 = selClassById(parentId);
		// }
		return tag2;
	}

	// 三级分类，未用
	public List getTag() {
		List list1 = new ArrayList();
		// 一级类
		List<BTag> tag1 = tag.selChild(0);
		for (BTag t1 : tag1) {
			Map map1 = new HashMap();
			map1.put("id", t1.getId());
			map1.put("tagName", t1.getTagName());
			// 二级类
			List<BTag> tag2 = tag.selChild(t1.getId());
			List list2 = new ArrayList();
			for (BTag t2 : tag2) {
				Map map2 = new HashMap();
				map2.put("id", t2.getId());
				map2.put("tagName", t2.getTagName());
				// 三级
				List<BTag> tag3 = tag.selChild(t2.getId());
				List list3 = new ArrayList();
				for (BTag t3 : tag3) {
					Map map3 = new HashMap();
					map3.put("id", t3.getId());
					map3.put("tagName", t3.getTagName());
					list3.add(map3);
				}
				map2.put("childs", list3);
				list2.add(map2);
			}
			map1.put("childs", list2);
			list1.add(map1);
		}
		return list1;
	}

	// 获取所有子类id
	public Set<Integer> getChildIds(BTag tags) {
		Set ids = new HashSet();
		ids.add(tags.getId());
		List<BTag> tagsChild = tag.selChild(tags.getId());
		// if(tagsChild.size()>0) {
		for (BTag tag1 : tagsChild) {
			Set<Integer> cids = getChildIds(tag1);
			for (Integer cid : cids) {
				ids.add(cid);
			}
			ids.add(tag1.getId());
		}
		;
		// }
		return ids;
	}

	/**
	 * 添加、编辑分类
	 */
	public int classSubmit(String id, String className, String parentId, String sequence, String remark, int userId) {
		int flag;
		BTag tag1 = new BTag();
		tag1.setTagName(className);
		tag1.setRemark(remark);
		tag1.setParentId(Integer.parseInt(parentId));
		tag1.setUserId(userId);
		if (sequence != "") {
			tag1.setSequence(Integer.parseInt(sequence));
		} else {
			tag1.setSequence(0);
		}
		if (id == null || "".equals(id)) {
			flag = tag.insert(tag1);
		} else {
			tag1.setId(Integer.parseInt(id));
			flag = tag.updateByPrimaryKey(tag1);
		}
		return flag;
	}

	/**
	 * 批量删除，单个删除
	 * 
	 * @param ids
	 * @return
	 */
	public int classDelete(String ids) {
		List<String> list = new ArrayList<String>();
		String[] str = ids.split(",");
		for (int i = 0; i < str.length; i++) {
			list.add(str[i]);
		}
		return tag.deleteByIds(list);
	}

	/**
	 * 添加笔记
	 * 
	 * @param title
	 * @param tagId
	 * @param keywords
	 * @param content
	 * @param userId
	 * @param noteState
	 *            权限
	 */
	public void addNoteByUserId(String title, Integer tagId, String keywords, String articleAbstract, String content,
			Integer noteState, Integer userId) {
		BNote record = new BNote();
		record.setUserId(userId);
		record.setTitle(title);
		record.setTagId(tagId);
		record.setKeywords(keywords);
		record.setArticleAbstract(articleAbstract);
		record.setContent(content);
		record.setNoteState(noteState);
		note.insertNoteByUserId(record);
	}

	/**
	 * 根据笔记id获取笔记的所有详细信息
	 * 
	 * @param id
	 * @return
	 */
	public BNote getNoteByNoteId(Integer id) {
		//增加浏览次数
		note.setViewsCountByNoteId(id);
		return note.selectByPrimaryKey(id);
	}

	/**
	 * 根据id更新笔记信息
	 * 
	 * @param id
	 * @param title
	 * @param tagId
	 * @param keywords
	 * @param articleAbstract
	 * @param content
	 */
	public void updateNoteByNoteId(Integer id, Integer noteState, String title, Integer tagId, String keywords,
			String articleAbstract, String content) {
		BNote record = new BNote();
		record.setId(id);
		record.setTitle(title);
		record.setTagId(tagId);
		record.setKeywords(keywords);
		record.setArticleAbstract(articleAbstract);
		record.setContent(content);
		record.setNoteState(noteState);
		note.updateByPrimaryKey(record);
	}

	/**
	 * 根据id查询标签名称,支持多级查询
	 * 
	 * @param id
	 * @return
	 */
	public List<BTag> getTagNamesById(Integer id) {
		return tag.getTagNamesById(id);
	}
}
